package com.yanqu.road.dao.impl.activity.mall;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.mall.UserActivityMallTouchData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserActivityMallTouchDataDaoImpl extends TempDataObjectDao<UserActivityMallTouchData> {

    public Map<Integer, Map<Integer, UserActivityMallTouchData>> getUserActivityMallTouchData(List<Integer> activityIdList) {
        String getUserActivityMallTouchDataSql = "select * from `t_u_activity_mall_touch_data` where activityId in(" + StringUtils.listToString(activityIdList, ",") + ")";
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, UserActivityMallTouchData>> resultMap = new ConcurrentHashMap<>();
        if(activityIdList.isEmpty()){
            return resultMap;
        }
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserActivityMallTouchDataSql);
            rs = call.executeQuery();
            while (rs.next()) {
                UserActivityMallTouchData data = parse(rs);
                if(!resultMap.containsKey(data.getActivityId())){
                    resultMap.put(data.getActivityId(), new ConcurrentHashMap<>());
                }
                resultMap.get(data.getActivityId()).put(data.getMallId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return resultMap;
    }

    public static String updateUserActivityMallTouchDataSql = "update `t_u_activity_mall_touch_data` set num=?,effectTime=? where activityId=? and mallId=?";
    @Override
    protected boolean updateData(UserActivityMallTouchData userActivityMallTouchData) {
        userActivityMallTouchData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userActivityMallTouchData.getNum()));
        para.put(i++, new DbParameter(Types.BIGINT, userActivityMallTouchData.getEffectTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userActivityMallTouchData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, userActivityMallTouchData.getMallId()));
        if (getDb().executeNoneQuery(updateUserActivityMallTouchDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserActivityMallTouchDataSql = "insert into `t_u_activity_mall_touch_data`(activityId,mallId,num,effectTime) values (?,?,?,?)";
    @Override
    protected boolean addData(UserActivityMallTouchData userActivityMallTouchData) {
        userActivityMallTouchData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userActivityMallTouchData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, userActivityMallTouchData.getMallId()));
        para.put(i++, new DbParameter(Types.INTEGER, userActivityMallTouchData.getNum()));
        para.put(i++, new DbParameter(Types.BIGINT, userActivityMallTouchData.getEffectTime()));
        if (getDb().executeNoneQuery(insertUserActivityMallTouchDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserActivityMallTouchData parseData(ResultSet rs) throws SQLException {
        UserActivityMallTouchData userActivityMallTouchData = new UserActivityMallTouchData();
        userActivityMallTouchData.setActivityId(rs.getInt("activityId"));
        userActivityMallTouchData.setMallId(rs.getInt("mallId"));
        userActivityMallTouchData.setNum(rs.getInt("num"));
        userActivityMallTouchData.setEffectTime(rs.getLong("effectTime"));

        return userActivityMallTouchData;
    }
}
